<?php
// database constants
// make sure the information is correct
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "password");
define("DB_NAME", "apptemplate");

// connection to the database
$dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS)
   or die("Unable to connect to MySQL");

// select a database to work with
$selected = mysql_select_db(DB_NAME, $dbhandle)
   or die("Could not select examples");

// return all available tables 
$result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle );

$tables = array();
while ($row = mysql_fetch_row($result_tbl)) {
   $tables[] = $row[0];
}

$arrTable = array();
//$arrTable[] = array("TableName"=>"users", "PrimaryKey"=>"UserID", "UserID", "Username", "Fullname", "Password", "UserType");
// iterate over each table and return the fields for each table
$i = 0;
foreach ( $tables as $table ) {
   //$output .= "<table name=\"$table\">";
   $result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );
   $arrTable[$i]["TableName"] = $table;
   while( $row1 = mysql_fetch_row($result_fld) ) {
      //$output .= "<field name=\"$row1[0]\" type=\"$row1[1]\"";
      //$output .= ($row1[3] == "PRI") ? " primary_key=\"yes\" />" : " />";
	  if ($row1[3] == "PRI") { // PrimaryKey
	  	$arrTable[$i]["PrimaryKey"] = $row1[0];
	  }
	$pos = strpos($row1[1], "(");
	if ($pos>0) 
		$arrTable[$i]['type'][] = substr($row1[1], 0, $pos);
	else
		$arrTable[$i]['type'][] = $row1[1];
	$arrTable[$i][] = $row1[0];
	  
   }
   $i++;
}

mysql_close($dbhandle);

echo "<pre>";
print_r($arrTable);
echo "</pre>";
?>